package com.briup.ch08;

import java.util.Objects;

/**
 * @Author briup-adam
 * @Date 2023/10/12 下午4:40
 * @Description
 **/

public class Student implements Comparable<Student>{
    private  String  name;
    private int age;
    static int count;
    private  int hashVal;

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", hashVal=" + hashVal +
                '}';
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Student() {
        count++;
        hashVal=count;
    }

    public Student(String name, int age) {
        this();
        this.name = name;
        this.age = age;
    }

    @Override
    public int hashCode() {
//        根据属性值 计算哈希码值
        return hashVal=Objects.hash(name, age);
//        return hashVal;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || this.getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return age == student.age &&
                name.equals(student.name);
    }

    @Override
//    this待排元素 比较   o已排元素
    public int compareTo(Student o) {
        return -(this.age -o.age);
    }
}
